home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Sessions / Movieless in QuickTime / SimpleCompress / SimpleCompress.c next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  3.0 KB  |  140 lines  |  [TEXT/CWIE]

  1. #include <MacTypes.h>
  2. #include <MacMemory.h>
  3. #include <Quickdraw.h>
  4. #include <Fonts.h>
  5. #include <Events.h>
  6. #include <Menus.h>
  7. #include <MacWindows.h>
  8. #include <TextEdit.h>
  9. #include <Dialogs.h>
  10. #include <OSUtils.h>
  11. #include <ToolUtils.h>
  12. #include <SegLoad.h>
  13. #include <Processes.h>
  14. #include <Movies.h>
  15. #include <QuickTimeComponents.h>
  16. #include <StandardFile.h>
  17. #include <NumberFormatting.h>
  18. #include <Files.h>
  19. #include <Endian.h>
  20.  
  21.     
  22. void Initialize(void);
  23. void CompressSomething( FSSpec *inFile, FSSpec *outFile, short outScript );
  24. void DoCompress(void);
  25.  
  26.  
  27. void main(void)
  28. {
  29.     Initialize();
  30.     
  31.     DoCompress();
  32. }
  33.  
  34.  
  35. void Initialize(void)
  36. {
  37.     InitGraf(&qd.thePort);
  38.     InitFonts();
  39.     InitWindows();
  40.     InitMenus();
  41.     TEInit();
  42.     InitDialogs(nil);
  43.     InitCursor();
  44. }
  45.  
  46. void CompressSomething( FSSpec *inFile, FSSpec *outFile, short outScript )
  47. {
  48.     OSErr err = 0;
  49.     DataCompressorComponent ci = 0;
  50.     short fref = 0;
  51.     long uncompressedLength = 0;
  52.     Handle uncompressedH = nil;
  53.     long compressedLength = 0;
  54.     Handle compressedH = nil;
  55.     unsigned long ignoreThis = 0;
  56.     long count;
  57.     
  58.     err = FSpOpenDF( inFile, fsRdPerm, &fref );
  59.     if( err ) goto bail;
  60.     
  61.     err = GetEOF( fref, &uncompressedLength );
  62.     if( err ) goto bail;
  63.     
  64.     uncompressedH = NewHandle( uncompressedLength );
  65.     err = MemError();
  66.     if( err ) goto bail;
  67.     HLock( uncompressedH );
  68.     
  69.     count = uncompressedLength;
  70.     err = FSRead( fref, &count, *uncompressedH );
  71.     if( err ) goto bail;
  72.     
  73.     FSClose( fref );
  74.     fref = 0;
  75.     
  76.     err = FSpDelete( outFile );
  77.     if( err == fnfErr ) err = noErr;
  78.     if( err ) goto bail;
  79.     
  80.     err = FSpCreate( outFile, 'ZOT ', 'ZOT ', outScript );
  81.     if( err ) goto bail;
  82.     
  83.     err = FSpOpenDF( outFile, fsWrPerm, &fref );
  84.     if( err ) goto bail;
  85.     
  86.     err = OpenADefaultComponent( DataCompressorComponentType, zlibDataCompressorSubType, &ci );
  87.     if( err ) goto bail;
  88.     
  89.     err = DataCodecGetCompressBufferSize( ci, uncompressedLength, (UInt32*) &compressedLength );
  90.     if( err ) goto bail;
  91.     
  92.     compressedH = NewHandle( compressedLength );
  93.     err = MemError();
  94.     if( err ) goto bail;
  95.     HLock( compressedH );
  96.     
  97.     err = DataCodecCompress( ci, 
  98.             *uncompressedH, uncompressedLength, 
  99.             *compressedH, compressedLength, (UInt32*) &compressedLength, &ignoreThis );
  100.     if( err ) goto bail;
  101.     
  102.     // Store the uncompressed size of the compressed data first.
  103.     uncompressedLength = EndianS32_NtoB( uncompressedLength );
  104.     count = 4;
  105.     err = FSWrite( fref, &count, &uncompressedLength );
  106.     if( err ) goto bail;
  107.     
  108.     count = compressedLength;
  109.     err = FSWrite( fref, &count, *compressedH );
  110.     if( err ) goto bail;
  111.     
  112. bail:
  113.     if( fref )
  114.         FSClose( fref );
  115.     if( ci )
  116.         CloseComponent( ci );
  117.     if( uncompressedH )
  118.         DisposeHandle( uncompressedH );
  119.     if( compressedH )
  120.         DisposeHandle( compressedH );
  121.     
  122.     if( err )
  123.         DebugStr( "\p oops." );
  124. }
  125.  
  126.  
  127. void DoCompress(void)
  128. {
  129.     StandardFileReply    inreply, outreply;
  130.     OSType                list[1] = { 'TEXT' };
  131.     
  132.     StandardGetFile( nil, 1, list, &inreply );
  133.     if( !inreply.sfGood ) return;
  134.     
  135.     StandardPutFile( "\pSave compressed file as:", "\puntitled.zot", &outreply );
  136.     if( !outreply.sfGood ) return;
  137.     
  138.     CompressSomething( &inreply.sfFile, &outreply.sfFile, outreply.sfScript );
  139. }
  140.